<?php
/**
 * Created by PhpStorm.
 * User: lyon
 * Date: 16-12-6
 * Time: 下午12:38
 */
class Curler
{
    protected static $ip;
    protected static $cookie_arr_login = array(
        'ALF' => '1513666765',
        'Apache' => '5136360922296.226.1482149866143',
        'SCF' => 'AhJaknGQluYdtKzfCDOBriRfDffMLAP6h0DhOnJBFV1oUJ2ewo82zJ-iDGH2i9DL_tgVfHjbjQ36LruoWo7XDIU.',
        'SINAGLOBAL' =>	'3346921005132.6035.1480748495787',
        'SSOLoginState' => '1482133735',
        'SUB' => '_2A251U-C3DeRxGeNI7lcZ8i3MzDmIHXVWKVV_rDV8PUJbmtBeLUrYkW8Yc4ppix2nSA7Ff1NwdMX-xdwJBg..',
        'SUBP' => '0033WrSXqPxfM725Ws9jqgMF55529P9D9W5.CThM4xEPFdNX4naQPVQQ5JpX5o2p5NHD95QfSo-f1hz0ehMfWs4DqcjZxsLV9cpJeK.4S5tt',
        'SUHB' => '0S7C15j7l21rjh',
//        'SWB' => 'usrmdinst_10',
        //'ULOGIN_IMG' => '14806067783599',
        'ULV' => '1482149866186:23:23:2:5136360922296.226.1482149866143:1482032268005',
        'UOR' => 'www.lupaworld.com,widget.weibo.com,www.csdn.net',
        'WBStorage' => '2c466cc84b6dda21|undefined',
        'WBtopGlobal_register_version' =>'81b5567db8ceeb18',
        'YF-Page-G0' => '280e58c5ca896750f16dcc47ceb234ed',
        'YF-Ugrow-G0' =>'ad06784f6deda07eea88e095402e4243',
        'YF-V5-G0' => '020421dd535a1c903e89d913fb8a2988',
        '_T_WM' => '5543e85dfb626ec14ab19f6b2888897e',
        '_s_tentry' => 'www.doc88.com',
        //'login_sid_t' => 'db355e7c77a18eeef77a0487006f86d9',
        'wvr' => '6',
        'un' => '13041240048'
    );
    public static function init()
    {
    }
    public static function getCookie()
    {
        $cookie = '';
        foreach (self::$cookie_arr_login as $key => $value)
        {
            if ($key != 'un')
            {
                $cookie .= $key . '=' . $value . ';';
            }
            else
            {
                $cookie .= $key . '=' . $value;
            }
        }
        return $cookie;
    }
    public static function request($method, $url, $fields=array(), $flag = 0)
    {
        //$user_agent_firefox = 'Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:50.0) Gecko/20100101 Firefox/50.0';
        $user_agent_chrome = 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883.75 Safari/537.36';
        if ($flag == 1 || self::$ip == '')
        {
            $rand_ip = self::get_rand_ip();
            self::$ip = $rand_ip;
        }
        else
        {
            $rand_ip = self::$ip;
        }
        $header = array(
            "CLIENT-IP:$rand_ip",
            "X-FORWARDED-FOR:$rand_ip",
        );
        $ch = curl_init($url);
        curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
        curl_setopt($ch, CURLOPT_COOKIE, self::getCookie());
        curl_setopt($ch, CURLOPT_USERAGENT, $user_agent_chrome);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
        curl_setopt($ch, CURLOPT_TIMEOUT, 10);
        curl_setopt($ch, CURLOPT_REFERER, "http://www.163.com/"); //构造来路
        curl_setopt($ch, CURLOPT_HEADER, 1);
        if ($method === 'POST')
        {
            curl_setopt($ch, CURLOPT_POST, true );
            curl_setopt($ch, CURLOPT_POSTFIELDS, $fields);
        }
        $result = curl_exec($ch);
        return $result;
    }
    public static function get_rand_ip()
    {
        $arr_1 = array("218","218","66","66","218","218","60","60","202","204","66","66","66","59","61","60","222","221","66","59","60","60","66","218","218","62","63","64","66","66","122","211");
        $randarr= mt_rand(0,count($arr_1)-1);
        $ip1id = $arr_1[$randarr];
        $ip2id=  round(rand(600000,  2550000)  /  10000);
        $ip3id=  round(rand(600000,  2550000)  /  10000);
        $ip4id=  round(rand(600000,  2550000)  /  10000);
        return  $ip1id . "." . $ip2id . "." . $ip3id . "." . $ip4id;
    }
}